package com.evaluationSystem.controller;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.write.metadata.style.WriteCellStyle;
import com.alibaba.excel.write.style.HorizontalCellStyleStrategy;
import com.evaluationSystem.config.R;
import com.evaluationSystem.entity.Appraise;
import com.evaluationSystem.entity.Score;
import com.evaluationSystem.entity.dto.ScoreDto;
import com.evaluationSystem.entity.vo.AnalyseInfoVo;
import com.evaluationSystem.entity.vo.AnalyseOwnVo;
import com.evaluationSystem.entity.vo.ScoreVo;
import com.evaluationSystem.service.AppraiseService;
import com.evaluationSystem.service.ScoreService;
import lombok.RequiredArgsConstructor;
import org.apache.poi.ss.usermodel.HorizontalAlignment;
import org.jetbrains.annotations.NotNull;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/score")
@RequiredArgsConstructor
public class ScoreController {
    //得分控制器
    private final ScoreService scoreService;
    private final AppraiseService appraiseService;
    // todo 保存用户的问卷评分
    @RequestMapping(value = "/saveScoring", method = RequestMethod.POST)
    public R<Score> saveScoring(@RequestBody Score score) {
        scoreService.saveScoring(score);
        return R.success();
    }


    @RequestMapping(value = "/scoreResult", method = RequestMethod.POST)
    public R<Double> scoreResult(@RequestBody Score score) {
//        appraiseService.deleteQuestionId(score.getQuestionId());

        Appraise appraise = new Appraise();
        appraise.setAppraiserPhone(score.getUserId());
        appraise.setQuestionId(score.getQuestionId());
        appraise.setAppraiserName(score.getUserName());
        appraise.setAssessedName(score.getReviewerName());
        double quanzhong = 0;
        AnalyseOwnVo evaluateObject = appraiseService.getEvaluateObjects(appraise);
        if (evaluateObject != null && evaluateObject.getAppraiserUser() != null) {
            for (AnalyseOwnVo.AppraiserUser appraiserUser : evaluateObject.getAppraiserUser()) {
                if (appraiserUser.getAssessedId().equals(score.getReviewerId())) {
                    quanzhong = appraiserUser.getQuanzhong();
                    System.out.println("此时的权重为：" + quanzhong);
                    break;
                }
            }
        }
        double v = scoreService.saveScorResult(score,quanzhong);
        return R.success(v);
    }


    @RequestMapping(value = "/scoringAnalyse", method = RequestMethod.POST)
    public R<Map<String, Object>> scoringAnalyse(@RequestBody ScoreDto scoreDto) {
        return R.success(scoreService.scoringAnalysePage(scoreDto));
    }

    @RequestMapping(value = "/getAnalyseInfo", method = RequestMethod.POST)
    public R<AnalyseInfoVo> getAnalyseInfo(@RequestBody ScoreDto scoreDto) {
        return R.success(scoreService.getAnalyseInfo(scoreDto));
    }

//    @RequestMapping(value = "/scoringAnalyse", method = RequestMethod.POST)
//    public R<PageInfo<ScoreVo>> scoringAnalyse(@RequestBody ScoreDto scoreDto) {
//        return R.success(scoreService.scoringAnalyse(scoreDto));
//    }

    @RequestMapping(value = "/exportAnalyse", method = RequestMethod.GET)
    public void exportAnalyse(@NotNull HttpServletResponse response, ScoreDto scoreDto) throws IOException {

        // 这里URLEncoder.encode可以防止中文乱码 当然和easy excel没有关系
        String fileName = URLEncoder.encode("Analyse", String.valueOf(StandardCharsets.UTF_8)).replaceAll("\\+", "%20");
        List<ScoreVo> list = scoreService.scoringAnalyse(scoreDto);
//        AtomicInteger i = new AtomicInteger();
//        list.getList().forEach(s -> s.setColumNo(i.getAndIncrement()));
        //设置头部样式
        WriteCellStyle headWriteCellStyle = new WriteCellStyle();
        //设置头部标题居中
        headWriteCellStyle.setHorizontalAlignment(HorizontalAlignment.CENTER);
        //内容策略
        WriteCellStyle contentWriteCellStyle = new WriteCellStyle();
        //设置 水平居中
        contentWriteCellStyle.setHorizontalAlignment(HorizontalAlignment.CENTER);
        // 这个策略是 头是头的样式 内容是内容的样式 其他的策略可以自己实现
        HorizontalCellStyleStrategy horizontalCellStyleStrategy = new HorizontalCellStyleStrategy(headWriteCellStyle, contentWriteCellStyle);

        response.setContentType("application/vnd.ms-excel");
        response.setCharacterEncoding("utf-8");
        response.setHeader("Content-disposition", "attachment;filename=" + fileName + ".xlsx");
        EasyExcel.write(response.getOutputStream(), ScoreVo.class).sheet("data").registerWriteHandler(horizontalCellStyleStrategy).doWrite(list);
    }

    @RequestMapping(value = "/scoringDetail", method = RequestMethod.POST)
    public R<Map<String, Object>> scoringDetail(@RequestBody ScoreDto scoreDto) {
        return R.success(scoreService.scoringDetail(scoreDto));
    }

    @RequestMapping(value = "/scoringSubsidiary", method = RequestMethod.POST)
    public R<Map<String, Object>> scoringSubsidiary(@RequestBody ScoreDto scoreDto) {
        return R.success(scoreService.scoringSubsidiary(scoreDto));
    }

    @RequestMapping(value = "/scoringExport", method = RequestMethod.GET)
    public void scoringExport(ScoreDto scoreDto, @NotNull HttpServletResponse response) throws IOException {
        scoreService.scoringExport(response, scoreDto);
    }
}
